app.controller("examSubjectAnswerCtrl",function($scope,$state,$http,$ionicLoading){
    var QuestionNum=null;//试题序号
    var QuestionId=null;//试题ID
    $scope.followShow = true;
    var user_info = JSON.parse(localStorage.getItem("UserInfo"));
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    angular.element(document).ready(function(){
        $scope.checkQuestion();
    })

    //读取试题
    $scope.checkQuestion = function (QuestionOrder) {

        //试题切换
        if(QuestionOrder == 'previous'){
            QuestionNum= QuestionNum - 1;
            if(QuestionNum != exam_subject.questionCnt){
                $scope.followShow = true;
            }
        }else if(QuestionOrder == 'follow'){
            QuestionNum= QuestionNum + 1;
            if(QuestionNum == exam_subject.questionCnt){
                $scope.followShow = false;
            }
        }else {
            QuestionNum=1;
        }
        if(QuestionNum == 0){
            QuestionNum= QuestionNum + 1;
            $ionicLoading.show({
                template: "已经是第一道试题了！",
                duration: 3000
            });
        }else{
            var url = getPath.url+getPath.name+"/app/findQuestion";
            $http({
                url: url,
                method: "POST",
                headers:{'Content-Type':'application/x-www-form-urlencoded'},
                data: {
                    userId:user_info.userId ,
                    examCircle:exam_subject.examCode,
                    examOrder:QuestionNum
                }
            }).success(function (data) {

                    // $scope.findExamUserAnaly(QuestionId);
                    $scope.nQuestionCnt=data.nQuestionCnt;

                    data=data.examQuestionDTO;
                    if (data.examOrder != null) {
                        // $scope.examListData = data;
                        QuestionId=data.chooseAnswerList[0].questionId;
                        if (data.type=="0"){
                            //清除上一次绑定的内容
                            $scope.chooseAnswerListCheckbox=null;
                            $scope.chooseAnswerListRadio=null;
                            //绑定数据
                            $scope.type='单选题';
                            // $scope.examOrder=data.examOrder;
                            // $scope.questionContent=data.questionContent;
                            // for(i=0;i<data.chooseAnswerList.length;i++){
                            //     if((data.userAnswerList!=null)&&(data.userAnswerList.indexOf(data.chooseAnswerList[i].answerCode)!=-1)){
                            //         data.chooseAnswerList[i].isTrue=1;
                            //     }else {
                            //         data.chooseAnswerList[i].isTrue=0;
                            //     }
                            // }
                            $scope.chooseAnswerListRadio=data.chooseAnswerList;
                        }else  if (data.type=="1"){
                            //清除上一次绑定的内容
                            $scope.chooseAnswerListRadio=null;
                            $scope.chooseAnswerListCheckbox=null;
                            //绑定数据
                            $scope.type='多选题';
                            // $scope.examOrder=data.examOrder;
                            // $scope.questionContent=data.questionContent;
                            // for(i=0;i<data.chooseAnswerList.length;i++){
                            //     if((data.userAnswerList!=null)&&(data.userAnswerList.indexOf(data.chooseAnswerList[i].answerCode)!=-1)){
                            //         data.chooseAnswerList[i].isTrue=1;
                            //     }else {
                            //         data.chooseAnswerList[i].isTrue=0;
                            //     }
                            // }
                            $scope.chooseAnswerListCheckbox=data.chooseAnswerList;

                        }else  if (data.type=="2"){
                            //清除上一次绑定的内容
                            $scope.chooseAnswerListCheckbox=null;

                            //绑定数据
                            $scope.type='判断题';
                            // $scope.examOrder=data.examOrder;
                            // $scope.questionContent=data.questionContent;
                            //判断题，判断上次答题结果
                            
                                if (data.chooseAnswerList[0].isTrue==1){
                                    $scope.chooseAnswerListRadio=[{answerCode:"A",answerContent:"正确",isTrue:1},{answerCode:"B",answerContent:"错误",isTrue:0}]
                                }else if(data.chooseAnswerList[0].isTrue==0){
                                    $scope.chooseAnswerListRadio=[{answerCode:"A",answerContent:"正确",isTrue:0},{answerCode:"B",answerContent:"错误",isTrue:1}]
                                }else {
                                    $scope.chooseAnswerListRadio=[{answerCode:"A",answerContent:"正确",isTrue:0},{answerCode:"B",answerContent:"错误",isTrue:0}]
                                }

                        }
                        $scope.examOrder=data.examOrder;
                        $scope.questionContent=data.questionContent;
                        $scope.resolve=data.resolve;
                        //判断题将0转换成A，将1转换成B
                        if (data.type=="2"){
                            if(data.userAnswerList == null){
                                $scope.userAnswerList='';
                            }else if(data.userAnswerList.indexOf('0')!=-1){
                                $scope.userAnswerList='A';
                            }else if(data.userAnswerList.indexOf('1')!=-1){
                                $scope.userAnswerList='B';
                            }
                        }else {
                            $scope.userAnswerList = data.userAnswerList;
                        }

                        // userAnswerNew = userAnswer;
                    } else {
                        $ionicLoading.show({
                            template: "没有更多考试题！",
                            duration: 3000
                        });
                    }
                })
                .error(function () {
                    $ionicLoading.hide();
                    $ionicLoading.show({
                        template: "获取数据失败，请联系管理员!",
                        duration: 3000
                    });
                });
        }


    }
    $scope.findExamUserAnaly = function (QuestionId) {
        var url = getPath.url+getPath.name+"/app/findExamUserAnaly";
        $http({
            url: url,
            method: "POST",
            headers:{'Content-Type':'application/x-www-form-urlencoded'},
            data: {
                userId:user_info.userId ,
                questionId:QuestionId
            }
        }).success(function (data) {
                if (data.resultCode == 200) {

                } else {
                    QuestionNum= QuestionNum - 1;
                    $ionicLoading.show({
                        template: data.result,
                        duration: 3000
                    });
                }
            })
            .error(function () {
                $ionicLoading.hide();
                $ionicLoading.show({
                    template: "登录失败",
                    duration: 3000
                });
            });
    }
    
    //返回试卷目录
    $scope.openExamCatalogueAnswer = function () {
        $state.go("home.examList");
    }
});